package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.BtyZxMdgz;
import cn.stylefeng.guns.sys.modular.system.entity.ZxChengbenMingxi;
import cn.stylefeng.guns.sys.modular.system.mapper.*;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyZxMdgzParam;
import cn.stylefeng.guns.sys.modular.system.model.params.ZxChengbenMingxiParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyZxMdgzResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyZxMdgzService;
import cn.stylefeng.guns.sys.modular.system.service.ZxChengbenMingxiService;
import cn.stylefeng.roses.core.reqres.response.ErrorResponseData;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2021-09-08
 */
@Service
public class BtyZxMdgzServiceImpl extends ServiceImpl<BtyZxMdgzMapper, BtyZxMdgz> implements BtyZxMdgzService {

    @Autowired
    private BtyBrandMapper btyBrandMapper;
    @Resource
    private MdzxMapper mdzxMapper;
    @Resource
    private BtyBxdaMapper btyBxdaMapper;
    @Resource
    private ZxChengbenMingxiService zxChengbenMingxiService;
    @Resource
    private ZxChengbenMingxiMapper zxChengbenMingxiMapper;

    @Override
    public void add(BtyZxMdgzParam param){
        BtyZxMdgz entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BtyZxMdgzParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BtyZxMdgzParam param){
        BtyZxMdgz oldEntity = getOldEntity(param);
        BtyZxMdgz newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BtyZxMdgzResult findBySpec(BtyZxMdgzParam param){
        return null;
    }

    @Override
    public List<BtyZxMdgzResult> findListBySpec(BtyZxMdgzParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BtyZxMdgzParam param){
        Page pageContext = getPageContext();
        if (ToolUtil.isNotEmpty(param.getFlag())){
            String flag = param.getFlag().replace("-","");
            param.setFlag(flag);
        }
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 获取装修工资
     * @return
     */
    @Override
    public ResponseData getGz(String time) {
        time = time.replace("-","");

        List<Map> list = btyBrandMapper.getGzhj(time);
        List<BtyZxMdgz> btyList = new ArrayList<>();

        for (Map map : list) {

            if (ToolUtil.isNotEmpty(map.get("kh"))){

                BtyZxMdgz btyZxMdgz = new BtyZxMdgz();
                btyZxMdgz.setYaoma(map.get("yaoma").toString());
                btyZxMdgz.setName(map.get("dm").toString());
                btyZxMdgz.setCost(new BigDecimal(map.get("gzhj").toString()));
                btyZxMdgz.setKemuId(1325729804877959170L);
                btyZxMdgz.setKeywordId(1240);
                btyZxMdgz.setStatus("2");
                btyZxMdgz.setFlag(time);
                btyZxMdgz.setKh(map.get("kh").toString());

                //门店工资表中没有该数据，新增
                int con = baseMapper.getGz(btyZxMdgz);
                if (con == 0){
                    btyList.add(btyZxMdgz);
                }
            }
        }

        if (ToolUtil.isNotEmpty(btyList)){
            //每150条保存一次
            for(int i = 0; i < btyList.size();){
                int i2 = (i + 150);
                i2 = i2 > btyList.size() ? btyList.size() : i2;
                List<BtyZxMdgz> sub = btyList.subList(i, i2);
                this.baseMapper.add(sub);
                i = i2;
            }
        }
        return ResponseData.success();
    }

    /**
     * 生成报销单
     * @param param
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData addBxd(BtyZxMdgzParam param) {
        //获取btyZxMdzxId
        String btyZxMdzxId = mdzxMapper.getMdzxId(param.getYaoma());
        if (ToolUtil.isEmpty(btyZxMdzxId)){
            return ResponseData.error("门店装修中没有此数据，不能保存！");
        }
        //获取报销人
        Map bxr = btyBxdaMapper.getBxr(param.getKh());
        if (ToolUtil.isEmpty(bxr)){
            return ResponseData.error("没有报销人，不能保存！");
        }

        ZxChengbenMingxiParam mingxiParam = new ZxChengbenMingxiParam();
        mingxiParam.setBtyyBxdaIdView(bxr.get("xm").toString());
        mingxiParam.setBtyyBxdaId(bxr.get("id").toString());
        mingxiParam.setBtyZxMdzxIds(btyZxMdzxId);
        mingxiParam.setKeywordId(param.getKeywordId());
//        mingxiParam.setShuliang();
//        mingxiParam.setUnitPrice();
        mingxiParam.setJine(param.getCost());
        mingxiParam.setStatus("6");//预留
        mingxiParam.setSrcType("bty_zx_mdgz");
        mingxiParam.setSrcValue(param.getId().toString());
        zxChengbenMingxiService.addMany(mingxiParam);
        param.setStatus("1");

        BtyZxMdgz oldEntity = getOldEntity(param);
        BtyZxMdgz newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        baseMapper.updateById(newEntity);
        return ResponseData.success();
    }

    /**
     * 撤回报销单
     * @param param
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData rebutBxd(BtyZxMdgzParam param) {
        //查询报销单状态 status = 6才可删除
        ZxChengbenMingxiParam mingxiParam = new ZxChengbenMingxiParam();
        mingxiParam.setSrcType("bty_zx_mdgz");
        mingxiParam.setSrcValue(param.getId().toString());
        ZxChengbenMingxi mingxi = zxChengbenMingxiMapper.getId(mingxiParam);

        if (mingxi.getStatus().equals("6")){
            ZxChengbenMingxiParam param1 = new ZxChengbenMingxiParam();
            param1.setBtyZxChengbenMingxiId(mingxi.getBtyZxChengbenMingxiId());
            boolean isDel = zxChengbenMingxiService.deleteBxd(param1);

            if(isDel == false){
                return new ErrorResponseData("撤回报销单失败");
            }
            //门店工资表中修改数据
            param.setStatus("2");

            BtyZxMdgz oldEntity = getOldEntity(param);
            BtyZxMdgz newEntity = getEntity(param);
            ToolUtil.copyProperties(newEntity, oldEntity);
            baseMapper.updateById(newEntity);

        }else {
            String sta = "";

            if (mingxi.getStatus().equals("5")){
                sta = "被打回";
            }else if (mingxi.getStatus().equals("3")){
                sta = "待审核";
            }else if (mingxi.getStatus().equals("2")){
                sta = "待打款";
            }else if (mingxi.getStatus().equals("1")){
                sta = "已打款";
            }else if (mingxi.getStatus().equals("9")){
                sta = "负单";
            }else if (mingxi.getStatus().equals("10")){
                sta = "已删除";
            }
            return ResponseData.error("该报销单已处于" + sta + "状态，不可撤回！");
        }

        return ResponseData.success();
    }

    @Override
    public List<Map> dmList() {

        List<Map> list = btyBrandMapper.dmMdgz();
        return list;
    }

    private Serializable getKey(BtyZxMdgzParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BtyZxMdgz getOldEntity(BtyZxMdgzParam param) {
        return this.getById(getKey(param));
    }

    private BtyZxMdgz getEntity(BtyZxMdgzParam param) {
        BtyZxMdgz entity = new BtyZxMdgz();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
